package com.yunhe.servlet;

import java.util.List;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yunhe.bean.CommodityClassify;
import com.yunhe.bean.page;
import com.yunhe.test.test;

/**
 * Servlet implementation class showProduct_Servlet
 */
@WebServlet("/showProduct_Servlet")
public class showProduct_Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public showProduct_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		//增加商品操作
		if("addproduct".equals(request.getParameter("manage"))){
			 //获取所有小类信息
			 test te =new test();
			 List<CommodityClassify>  list=te.ShowSmallClassify();
			 System.out.println(te.ShowSmallClassify());
			 request.setAttribute("SmallClassify", list);
			 request.getRequestDispatcher("manage/product_add.jsp").forward(request, response);		     
		}
		//修改商品操作
		if("change".equals(request.getParameter("manage"))){
			String cid=request.getParameter("cid");
			 //获取所有小类信息
			 test te =new test();
			 java.util.List<CommodityClassify> list=te.ShowSmallClassify();
			 System.out.println(list);
			 request.setAttribute("SmallClassify", list);
			 request.setAttribute("cid", cid);
			 request.getRequestDispatcher("manage/product-modify.jsp").forward(request, response);
			
		}
		
		
		//遍历商品
		else{
			test test=new test();
			//设置默认页面数为1
			int defaultPage=1;
			//判断是否是首次进入该页面,如果不是则改变默认页面数
			if(request.getParameter("currentPage")!=null){
				defaultPage=Integer.parseInt(request.getParameter("currentPage"));
			}
			//设置一个page对象
			page pg=new page();
			pg.setCurrentPage(defaultPage);
			int countCommodity=test.countc();
			pg.setTotalNews(countCommodity);
			int totalPage=pg.getTotalPage();
			//引用bus对象及方法		
			Map map=new HashMap();
			ArrayList productList=new ArrayList();
			productList=(ArrayList) test.showc(pg);
			map.put("productList",productList);
			map.put("totalPage", totalPage);
			map.put("page", pg);
			//传递map到前端
			request.setAttribute("map",map);
			//页面转向到前端
		    request.getRequestDispatcher("manage/product.jsp").forward(request, response);
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
